package com.hup.utils.selenium;

import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.SearchContext;
import org.openqa.selenium.WebElement;

import java.util.Arrays;
import java.util.List;

/**
 * @author hugan
 * @date 2020/3/20
 */
public class ElementUtil {

    /**
     * @param bys length>=1;前n个条件都是找唯一的元素,最后一个条件是找数组
     * @return emptyAble
     */
    public static List<WebElement> findElements(SearchContext searchContext, By... bys) {
        if (bys.length == 1) return searchContext.findElements(bys[0]);

        WebElement element = null;
        for (int i = 0; i < bys.length - 1; i++) {
            By by = bys[i];
            try {
                if (i == 0) {
                    element = searchContext.findElement(by);
                } else {
                    element = element.findElement(by);
                }
            } catch (NoSuchElementException e) {
                element = null;
            }
            if (element == null) {
                throw new IllegalArgumentException("找不到:" + by);
            }
        }
        //noinspection ConstantConditions
        return element.findElements(bys[bys.length - 1]);
    }

    /**
     * @param bys length>=1;前n个条件都是找唯一的元素,最后一个条件是找数组
     * @return notNull
     */
    public static WebElement findElement(SearchContext searchContext, By... bys) {
        List<WebElement> res = findElements(searchContext, bys);
        if (res.isEmpty()) {
            throw new IllegalArgumentException("找不到:" + bys[bys.length - 1]);
        }
        return res.get(0);
    }

    /**
     * @param bys length>=1;前n个条件都是找唯一的元素,最后一个条件是找数组
     * @return emptyAble
     */
    public static WebElement findElement(SearchContext searchContext, int targetIndex, By... bys) {
        List<WebElement> res = findElements(searchContext, bys);
        if (res.size() > targetIndex) return res.get(targetIndex);
        throw new IllegalArgumentException("找不到元素"+ Arrays.toString(bys)+", index=" + targetIndex + ", size=" + res.size());
    }

    /**
     * 根据文本找元素
     * @param tagName 元素的标签
     * @param text 包含的文本
     * @return notNull
     */
    public static WebElement findByText(SearchContext searchContext, String tagName, String text) {
        try {
            //要在'//'前加'.'才是在[searchContext]下查找,否则是全局查找
            return searchContext.findElement(By.xpath(".//" + tagName + "[contains(text(),'" + text + "')]"));
        } catch (NoSuchElementException e) {
            throw new IllegalArgumentException("找不到:" + text);
        }
    }

    /**
     * @param a a标签
     * @return a标签的超链接
     */
    public static String getUrl(WebElement a) {
        return a.getAttribute("href");
    }

    /**
     * @return null-NumberFormatException
     */
    public static Double getDoubleAllowErr(WebElement element) {
        try {
            /*
             * element.getText()
             *  isDisplayed为false的元素,那么getText()将为空:如页面上tab切换后,元素被隐藏了
             *   解决:使用getAttribute("innerHTML")
             */
            return Double.parseDouble(element.getAttribute("innerHTML"));
        } catch (NumberFormatException e) {
            return null;
        }
    }

    /**
     * @return null-NumberFormatException
     */
    public static Integer getIntegerAllowErr(WebElement element) {
        try {
            return Integer.parseInt(element.getAttribute("innerHTML"));
        } catch (NumberFormatException e) {
            return null;
        }
    }

}
